/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.deposit;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import src.HibernateUtil;
import src.domain.deposit.Bilyet;
import src.services.CRUEDService;
import src.services.saving.SavtrxModel;
import src.util.Utils;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class BilyetModel implements java.io.Serializable {
    private CRUEDService service;

    public BilyetModel() {
        service = new CRUEDService();
    }
    
    public List<Bilyet> findAll(Map<String, Object> filters, String orderby) {
        return (List<Bilyet>) service.findAllSQLAdvance("from Bilyet", filters, orderby);
    }
    
     //chan
    //kirim sql lengkap beserta where dan orderby
    public List<Bilyet> search(String SQLFull) {
        return (List<Bilyet>) service.findAllSQL(SQLFull, null);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Bilyet", "bilyetId","bilyetNo");
    }
    
    public boolean isNoBilyetExist(String value, Integer id) {
        BigDecimal count;
        String cond = " where bilyet_no = "+Utils.fmtSQLString(value);
        if (id != null) cond+=" and bilyet_id <> "+id;
        
        try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from bilyet "+cond);
        } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
    public Bilyet getByNo(String value) {
        Map<String, String> where = new HashMap<>();
        where.put("bil.bilyetNo", value);
        where.put("bil.closeDate", null);
        return (Bilyet) service.getObjectSQL("from Bilyet bil", where);
    }
    
     public Bilyet getById(Integer id) {
        return (Bilyet)  service.getById(Bilyet.class, id);
    }
     
    public Integer authorized(Integer id) {
         return (Integer) service.getObjectSQL(
                 "select authorizedUid from Bilyet where bilyetId = "+id, null);
    }
    
    public boolean saveData(Bilyet data) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        boolean success;
        
        try {
            session.saveOrUpdate(data);
            
            if (data.getAuthorizedUid()!=null && data.getCloseDate()==null) {
                session.flush();
                Query q = session.createSQLQuery(" { call deposito_modul.deposit_actions (?) }");
                q.setInteger(0,data.getBilyetId());  
                q.executeUpdate();
            }
            tx.commit();
            success = true;  
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {}
            }
            success = false;  
            Logger.getLogger(BilyetModel.class.getName()).log(Level.SEVERE, null, e);
        }
        
        return success;
    }
    
    public boolean deleteData(Bilyet data) {
        return service.deleteData(data);
    }
}
